package cool.tianyou.blue.bird.util;

import android.content.Context;
import android.os.Build;

import androidx.annotation.RequiresApi;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;

import java.util.ArrayList;
import java.util.List;

import cool.tianyou.blue.bird.Filter;

public class FilterManager {
    private Context context;
    private static final String SP_KEY="FILTER_LIST";
    private List<Filter> filterList = new ArrayList<>();

    public static final FilterManager manager = new FilterManager();

    public static FilterManager instance(Context context) {
        manager.context = context;
        manager.filterList = JSONArray.parseArray(SpUtil.getString(context,SP_KEY , "[]"), Filter.class);
        return manager;
    }

    public void add(Filter filter){
        this.filterList.add(filter);
        SpUtil.putString(context,SP_KEY, JSON.toJSONString(this.filterList));
    }
    public void remove(int index){
        this.filterList.remove(index);
        SpUtil.putString(context,SP_KEY, JSON.toJSONString(this.filterList));
    }
    @RequiresApi(api = Build.VERSION_CODES.N)
    public boolean pass(String pkgName, String title, String content){
        return filterList.stream().noneMatch(filter -> filter.match(pkgName, title, content));
    }

}
